package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.ReportIndexPictureBean;
import com.zx.kitchen.index.common.mvp.presenter.BusinessReportIndexPicPresenter;
import com.zx.kitchen.index.common.mvp.presenter.PurchaseReportIndexPicPresenter;
import com.zx.kitchen.index.common.mvp.view.BusinessReportIndexPicView;
import com.zx.kitchen.index.common.utils.ChartUtils;
import com.zx.kitchen.index.common.utils.TimeUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/17.
 */

public class ChartReportActivity extends Activity implements View.OnClickListener, BusinessReportIndexPicView {

    private LineChart spreadLineChart;
    private Spinner spType;
    private EditText etStartTime;
    private EditText etEndTime;
    private Button btnCount;
    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private BusinessReportIndexPicPresenter businessReportIndexPicPresenter;
    private PurchaseReportIndexPicPresenter purchaseReportIndexPicPresenter;
    private String reportType = "";
    private boolean isPurchase = false;

    private ArrayAdapter<String> mArrayAdapter;
    private static String[] mFontArrayString = new String[]{};

    String[] splitDay = new String[]{};
    String[] splitOrderMoney = new String[]{};
    String[] splitReceiveMoney = new String[]{};
    String[] splitDiscountMoney = new String[]{};

    //图表的
    // x轴显示的数据，这里默认使用数字下标显示
    ArrayList<String> xValues = new ArrayList<String>();
    // y轴的数据
    ArrayList<Entry> yValues = new ArrayList<Entry>();
    private TextView tvChartReportType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart_report);
        reportType = getIntent().getStringExtra("reportType");
        isPurchase = getIntent().getBooleanExtra("isPurchase", false);
        initView();
        initListener();
    }

    private void initListener() {
        btnCount.setOnClickListener(this);
        toolbarLeft.setOnClickListener(this);
    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));
        tvChartReportType = (TextView) findViewById(R.id.tv_chart_report_type);
        View headerSelectDate = findViewById(R.id.header_select_date);
        etStartTime = (EditText) headerSelectDate.findViewById(R.id.et_start_time);
        etEndTime = (EditText) headerSelectDate.findViewById(R.id.et_end_time);
        spType = (Spinner) findViewById(R.id.sp_type);
        spreadLineChart = (LineChart) findViewById(R.id.spread_line_chart);
        btnCount = (Button) findViewById(R.id.btn_count);
        etStartTime.setText(TimeUtil.getToday());
        etEndTime.setText(TimeUtil.getToday());
        if (reportType.equals("m")) {
            tvChartReportType.setText("单位时间：月");
        } else if (reportType.equals("w")) {
            tvChartReportType.setText("单位时间：周");
        } else {
            tvChartReportType.setText("单位时间：日");
        }

        mFontArrayString = new String[]{"应收金额", "实收金额", "优惠金额"};
        //        seeting_spinner改变了spinner的默认样式
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayString) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        spType.setAdapter(mArrayAdapter);
        businessReportIndexPicPresenter = new BusinessReportIndexPicPresenter(this, getBaseContext());
        purchaseReportIndexPicPresenter = new PurchaseReportIndexPicPresenter(this, getBaseContext());

        if (isPurchase) {
            toolbarTitle.setText("采购财务报表");
            purchaseReportIndexPicPresenter.getDataFromNet(reportType, "2017-01-01", TimeUtil.getToday());
        } else {
            toolbarTitle.setText("营业财务报表");
            businessReportIndexPicPresenter.getDataFromNet(reportType, "2017-01-01", TimeUtil.getToday());
        }

    }

    private void drawTheChartByMPAndroid() {
        LineData lineData = ChartUtils.getInstance().getLineData(xValues, yValues, this);//int count, float range,
        ChartUtils.getInstance().showChart(spreadLineChart, lineData, Color.rgb(255, 255, 255));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftLin:
                finish();
                break;
            case R.id.btn_count:
                startActivity(new Intent(this, BusinessReportActivity.class));
                break;
        }
    }

    @Override
    public void onSuccess(ReportIndexPictureBean reportIndexPictureBean) {
        if (null == reportIndexPictureBean.getDetail()) return;
        if (!TextUtils.isEmpty(reportIndexPictureBean.getDetail().getDay())) {
            splitDay = reportIndexPictureBean.getDetail().getDay().split(",");
        }
        if (!TextUtils.isEmpty(reportIndexPictureBean.getDetail().getOrder_money())) {
            splitOrderMoney = reportIndexPictureBean.getDetail().getOrder_money().split(",");
        }
        if (!TextUtils.isEmpty(reportIndexPictureBean.getDetail().getReceive_money())) {
            splitReceiveMoney = reportIndexPictureBean.getDetail().getReceive_money().split(",");
        }
        if (!TextUtils.isEmpty(reportIndexPictureBean.getDetail().getDiscount_money())) {
            splitDiscountMoney = reportIndexPictureBean.getDetail().getDiscount_money().split(",");
        }
        for (int i = 0; i < splitDay.length; i++) {
            xValues.add(splitDay[i]);
        }
        for (int i = 0; i < splitOrderMoney.length; i++) {
            float value = Float.parseFloat(splitOrderMoney[i]);
            xValues.clear();
            yValues.add(new Entry(value, i));
        }
        spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0://"应收金额",
                        for (int i = 0; i < splitOrderMoney.length; i++) {
                            float value = Float.parseFloat(splitOrderMoney[i]);
                            yValues.clear();
                            yValues.add(new Entry(value, i));
                        }
                        for (int i = 0; i < yValues.size(); i++) {
                            Log.d("ChartReport-----0", yValues.get(i).toString() + " yValues.size():" + yValues.size());
                        }
                        drawTheChartByMPAndroid();
                        break;
                    case 1://"实收金额",
                        for (int i = 0; i < splitReceiveMoney.length; i++) {
                            float value = Float.parseFloat(splitReceiveMoney[i]);
                            yValues.clear();
                            yValues.add(new Entry(value, i));
                        }
                        for (int i = 0; i < yValues.size(); i++) {
                            Log.d("ChartReport-----1", yValues.get(i).toString() + " yValues.size():" + yValues.size());
                        }
                        drawTheChartByMPAndroid();
                        break;
                    case 2://"优惠金额"
                        for (int i = 0; i < splitDiscountMoney.length; i++) {
                            float value = Float.parseFloat(splitDiscountMoney[i]);
                            yValues.clear();
                            yValues.add(new Entry(value, i));
                        }
                        for (int i = 0; i < yValues.size(); i++) {
                            Log.d("ChartReport-----2", yValues.get(i).toString() + " yValues.size():" + yValues.size());
                        }
                        drawTheChartByMPAndroid();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        drawTheChartByMPAndroid();
    }

    @Override
    public void onFail(String str) {

    }


}
